<?php

/**
 * Quan tri tin
 * 
 */
class News_model extends Model {
	
	private $table 	= 'news';
	
	function News_model() {
		parent::Model ();
	}
	
	function write_news( $type, $file = '' ) {
		
		$data = array(
			'id_lang'		=> get_admin_lang(),
			'type'			=> $type,
			'cat_id'		=> isset( $_POST['cat'] ) ? (int)$_POST['cat'] : 0,
			'name'			=> isset( $_POST['name'] ) ? htmlspecialchars( $_POST['name'] ) : '',
			'description'	=> isset( $_POST['description'] ) ? $_POST['description'] : '' ,
			'detail'		=> isset( $_POST['detail'] ) ? $_POST['detail'] : '' , 
			'source'		=> isset( $_POST['source'] ) ? htmlspecialchars( $_POST['source'] ) : '', 
			'authors'		=> isset( $_POST['author'] ) ? htmlspecialchars( $_POST['author'] ) : '', 
			'image'			=> isset( $_POST['pic'] ) ? htmlspecialchars( $_POST['pic'] ) : '', 
			'image_text'	=> isset( $_POST['picdesc'] ) ? htmlspecialchars( $_POST['picdesc'] ) : '',
			'status'		=> isset( $_POST['status'] ) ? (int)$_POST['status'] : 0,
			'new'			=> isset( $_POST['new'] ) ? (int)$_POST['new'] : 0,
			'focus'			=> isset( $_POST['focus'] ) ? (int)$_POST['focus'] : 0,			
			'home'			=> isset( $_POST['home'] ) ? (int)$_POST['home'] : 0,			
			'orders'		=> 0,
			'date'			=> time(),
			'file_attach'	=> $file
		);
		
		$this->db->insert( $this->table , $data );		
	}
	
	function list_news( $cat = 0, $type = 0, $num , $offset, &$total ) {
		$data = array ();
		
		if ( $cat == 0 )
			$where = '';
		else 
			$where = '`cat_id` = '.$cat.' AND ';
		
		$query = $this->db->query("SELECT `id`, `type`, `cat_id`, `name`, `description`, `detail`, `source`, `authors`, `image`, `image_text`, `status`, `new`, `focus`, `home`, `orders`, `date` FROM `$this->table` WHERE $where `type` = '$type' AND `id_lang` = '".get_admin_lang()."' ORDER BY `orders` ASC, `id` DESC LIMIT $offset, $num");
		
		if ( $query->num_rows () > 0 ) {
			$data = $query->result_array();
		}
		
		// Tông số bản ghi
		$query = $this->db->query("SELECT `id` FROM `$this->table` WHERE $where `id_lang` = '".get_admin_lang()."' AND `type` = '$type' ");
		$total = $query->num_rows();
		
		$query->free_result ();
		return $data;
	}

	function update_news_status( $total, $type ) {
	
		for( $k = 1; $k < $total ; $k++ ) {
			
			$status = @(int)$_POST['status_'.$k];
			$order 	= @(int)$_POST['order_'.$k];
			$home 	= @(int)$_POST['home_'.$k];
			$new 	= @(int)$_POST['new_'.$k];
			$focus 	= @(int)$_POST['focus_'.$k];
			
			$id		= @(int)$_POST['id_'.$k]; 
			
			$data = array(
				'status'	=> $status,
				'new'		=> $new,
				'focus'	=> $focus,
				'home'	=> $home,
				'orders'	=> $order,
			);
			
			$this->db->where('id', $id );	
			$this->db->where('id_lang', get_admin_lang() );
			$this->db->where('type', $type );
			
			$this->db->update( $this->table, $data);
		}
	}
	
	function select_news( $id = 0, $type = 0) {
		$data = array ();
		
		if ( is_numeric( $id ) ) {
			$query = $this->db->query("SELECT `id`, `type`, `cat_id`, `name`, `description`, `detail`, `source`, `authors`, `image`, `image_text`, `status`, `new`, `focus`, `home`, `orders`, `file_attach` FROM `$this->table` WHERE `type` = '$type' AND `id` = '$id' AND `id_lang` = '".get_admin_lang()."' LIMIT 1 ");
			
			if ( $query->num_rows () > 0 ) {
				$data = $query->row_array();
			}
			$query->free_result ();
		}
		return $data;
	}
	
	function update_news( $type, $file = '' ) {
		$id = (int)$_POST['hid'];
		
		$data = array(
			'id_lang'		=> get_admin_lang(),
			'type'			=> $type,
			'cat_id'		=> isset( $_POST['cat'] ) ? (int)$_POST['cat'] : 0,
			'name'			=> isset( $_POST['name'] ) ? htmlspecialchars( $_POST['name'] ) : '',
			'description'	=> isset( $_POST['description'] ) ? $_POST['description'] : '' ,
			'detail'		=> isset( $_POST['detail'] ) ? $_POST['detail'] : '' , 
			'source'		=> isset( $_POST['source'] ) ? htmlspecialchars( $_POST['source'] ) : '', 
			'authors'		=> isset( $_POST['author'] ) ? htmlspecialchars( $_POST['author'] ) : '',  
			'image'			=> isset( $_POST['pic'] ) ? htmlspecialchars( $_POST['pic'] ) : '', 
			'image_text'	=> isset( $_POST['picdesc'] ) ? htmlspecialchars( $_POST['picdesc'] ) : '',
			'status'		=> isset( $_POST['status'] ) ? (int)$_POST['status'] : 0,
			'new'			=> isset( $_POST['new'] ) ? (int)$_POST['new'] : 0,
			'focus'			=> isset( $_POST['focus'] ) ? (int)$_POST['focus'] : 0,			
			'home'			=> isset( $_POST['home'] ) ? (int)$_POST['home'] : 0
		);
		
		if ( $file != '' )
			$data['file_attach'] = $file;
				
		$this->db->where('id', $id );	
		$this->db->where('id_lang', get_admin_lang() );
		$this->db->where('type', $type );
		
		$this->db->update( $this->table, $data);		
	}
	
	function del_news( $id, $type ) {
		$this->db->where('id', $id );
		$this->db->where('type', $type );
		$this->db->where('id_lang', get_admin_lang() );
		$this->db->delete( $this->table );
	}
}
?>